using Deferred.Library.Deferred;
using Deferred.Library.Input;
using Deferred.Library.Scene;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Deferred.Library
{
    /// <summary>
    /// 
    /// </summary>
    public class DeferredRenderGame : Game
    {
        private KeyboardState currentKeyboardState;
        private IFocusHelper focusHelper;
        private DeferredRenderEngine renderEngine;
        private KeyboardHandler keyboardHandler;
        private SceneGraph scene;

        /// <summary>
        /// 
        /// </summary>
        protected GraphicsDeviceManager deviceManager;
        
        /// <summary>
        /// 
        /// </summary>
        protected DeferredBase DeferredBase { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        protected SceneGraph Scene { get { return scene; } set { scene = value; renderEngine.Scene = value; } }

        /// <summary>
        /// 
        /// </summary>
        public DeferredRenderGame()
            : base()
        {
            Content.RootDirectory = "Content";

            deviceManager = new GraphicsDeviceManager(this);

            ResourceContentManager internalContent = new ResourceContentManager(this.Services, LibraryContent.ResourceManager);

            DeferredBase = new DeferredBase(Services, new DualContentManager(internalContent, Content), deviceManager);

            scene = new SceneGraph();

            renderEngine = new DeferredRenderEngine(DeferredBase, scene);

            this.IsFixedTimeStep = false;
            deviceManager.SynchronizeWithVerticalRetrace = false;

            keyboardHandler = new KeyboardHandler(Services);
            this.Services.AddService(typeof(IKeyboardHandler), keyboardHandler);

            QuadRenderer quadRenderer = new QuadRenderer(deviceManager);
            Services.AddService(typeof(IQuadRenderer), quadRenderer);
        }

        /// <summary>
        /// 
        /// </summary>
        protected override void Initialize()
        {
            renderEngine.Initialize();
            keyboardHandler.Initialize();

            focusHelper = (IFocusHelper)Services.GetService(typeof(IFocusHelper));

            base.Initialize();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Update(GameTime gameTime)
        {
            keyboardHandler.Update(gameTime);

            updateCamera(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Draw(GameTime gameTime)
        {
            renderEngine.Draw(gameTime);

            base.Draw(gameTime);
        }

        private void updateCamera(GameTime gameTime)
        {
            if ((null == focusHelper || focusHelper.GameHasFocus) && scene.Camera != null)
            {
                currentKeyboardState = Keyboard.GetState();

                float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

                // Check for input to rotate the camera up and down around the model.
                if (currentKeyboardState.IsKeyDown(Keys.Up) ||
                    currentKeyboardState.IsKeyDown(Keys.W))
                {
                    scene.Camera.CameraArc += time * 0.1f;
                }

                if (currentKeyboardState.IsKeyDown(Keys.Down) ||
                    currentKeyboardState.IsKeyDown(Keys.S))
                {
                    scene.Camera.CameraArc -= time * 0.1f;
                }

                // Check for input to rotate the camera around the model.
                if (currentKeyboardState.IsKeyDown(Keys.Right) ||
                    currentKeyboardState.IsKeyDown(Keys.D))
                {
                    scene.Camera.CameraRotation += time * 0.1f;
                }

                if (currentKeyboardState.IsKeyDown(Keys.Left) ||
                    currentKeyboardState.IsKeyDown(Keys.A))
                {
                    scene.Camera.CameraRotation -= time * 0.1f;
                }

                // Check for input to zoom camera in and out.
                if (currentKeyboardState.IsKeyDown(Keys.Z))
                    scene.Camera.CameraDistance += time * 0.25f;

                if (currentKeyboardState.IsKeyDown(Keys.X))
                    scene.Camera.CameraDistance -= time * 0.25f;

                if (currentKeyboardState.IsKeyDown(Keys.R))
                {
                    scene.Camera.CameraArc = -30;
                    scene.Camera.CameraRotation = 0;
                    scene.Camera.CameraDistance = 100;
                }
            }
        }
    }
}
